﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.Routing;

namespace SimpComm
{
    static public partial class ElementExtensions
    {
        public static MvcHtmlString Niko_HT_Input_Number<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
            var name = metadata.PropertyName;
            var requited = metadata.IsRequired;
            StringBuilder sb = new StringBuilder();
            sb.Append("<div class=\"control-group\" id=\"" + name + "_CheckNumber\">");
            sb.Append("<label class=\"control-label\">");
            sb.Append(LabelExtensions.LabelFor<TModel, TProperty>(htmlHelper, expression));
            sb.Append("</label>");
            sb.Append("<div class=\"controls\">");
            sb.Append("<div class=\"input-prepend\">");
            sb.Append("<span class=\"add-on\">数字</span>");
            sb.Append(InputExtensions.TextBoxFor(htmlHelper, expression,
                                                 new
                                                     {
                                                         @class = "input-xlarge",
                                                         @placeholder = "请输入数字",
                                                         @onblur = "onblur_CheckNumber({note:'" + name + "_CheckNumber',required:" + (requited ? "true" : "false") + "})"
                                                     }));
            sb.Append("</div>");
            sb.Append("<p class=\"help-block\">");
            sb.Append(ValidationExtensions.ValidationMessageFor<TModel, TProperty>(htmlHelper, expression, (string)null, (IDictionary<string, object>)new RouteValueDictionary()));
            sb.Append("</p>");
            sb.Append("</div>");
            sb.Append("</div>");

            //
            //            sb.Append("<div class=\"control-group\">");
            //            sb.Append("<label class=\"control-label\">");
            //            var LabelHtmlString = LabelExtensions.LabelFor<TModel, TProperty>(htmlHelper, expression);
            //            sb.Append(LabelHtmlString);
            //            sb.Append("<div class=\"controls\">");
            //            var HiddenHtmlString = InputExtensions.HiddenFor<TModel, TProperty>(htmlHelper, expression, (IDictionary<string, object>)null);
            //            sb.Append(HiddenHtmlString);
            //            ModelMetadata metadata = ModelMetadata.FromLambdaExpression<TModel, TProperty>(expression, htmlHelper.ViewData);
            //            var name = metadata.PropertyName;
            //            sb.Append("<select class=\"input-xlarge\" id=\"" + name + "_Select\">");
            //            foreach (var item in DataBinds)
            //            {
            //                if (defaultValue.Equals(item.Value))
            //                {
            //                    sb.Append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
            //
            //                }
            //                else
            //                {
            //
            //                    sb.Append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
            //                }
            //            }
            //            var validat = ValidationExtensions.ValidationMessageFor<TModel, TProperty>(htmlHelper, expression, (string)null,
            //                                                                         (IDictionary<string, object>)
            //                                                                         new RouteValueDictionary());
            //            sb.Append(validat);
            //            sb.Append("</div>");
            //            sb.Append("</select>");
            //            sb.Append("<script type=\"text/javascript\">");
            //            sb.Append("$(\"#" + name + "_Select\").change(function (e) {");
            //            sb.Append("$(\"#" + name + "\").val($(\"#" + name + "_Select\").val());");
            //            sb.Append("});");
            //            sb.Append("</script>");
            return new MvcHtmlString(sb.ToString());
        }
    }
}
